package com.example.db2dm.runner;

import com.example.db2dm.entity.MigrationResult;
import com.example.db2dm.service.MigrationService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.sql.SQLException;
import java.util.List;

@Component
public class MigrationRunner implements CommandLineRunner {
    
    private static final Logger logger = LoggerFactory.getLogger(MigrationRunner.class);
    
    @Autowired
    private MigrationService migrationService;
    
    @Override
    public void run(String... args) throws Exception {
        logger.info("开始执行数据库迁移任务...");
        long startTime = System.currentTimeMillis();
        
        try {
            List<MigrationResult> results = migrationService.migrate();
            
            long endTime = System.currentTimeMillis();
            logger.info("数据迁移任务完成，总耗时: {} ms", (endTime - startTime));
            
        } catch (Exception e) {
            logger.error("执行数据迁移任务时发生错误: {}", e.getMessage(), e);
            // 即使发生错误也正常退出，避免应用启动失败
        }
    }
}